#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化3行3列
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    //设置列名
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("性别"));
    //初始化数据
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("男"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("2"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("女"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("3"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("保密"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_AddRow_clicked()
{
    //获取行数
    int rowCount = ui->tableWidget->rowCount();
    //添加行
    ui->tableWidget->insertRow(rowCount);
}



void Widget::on_pushButton_DelRow_clicked()
{
    //找到当前行
    int row = ui->tableWidget->currentRow();
    //删除
    ui->tableWidget->removeRow(row);
}



void Widget::on_pushButton_AddCol_clicked()
{
    //获取列数
    int colCount = ui->tableWidget->columnCount();
    ui->tableWidget->insertColumn(colCount);
    //设置列名
    const QString& text = ui->lineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(colCount,new QTableWidgetItem(text));
}


void Widget::on_pushButton_DelCol_clicked()
{
    int col = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(col);
}
